export function request(options) {
	return new Promise((reslove, reject) => {
		const token = uni.getStorageSync("token")
		const method = options.method ? options.method.toUpperCase() : 'GET'
		let url = options.url
		if (!/^http/.test(url)) {
			if (!/^\//.test(url)) {
				url = '/' + url
			}
			url = process.env.API_URL + url
		}
		if(process.env.DEV){
			url += ".json"
		}
		//console.log(url);
		uni.showLoading({
			title: '正在操作'
		})
		try{
			uni.request({
				url: url,
				data: options.data || undefined,
				header: {
					"Authorization": "Bearer " + token
				},
				method: options.method,
				success(res) {
					// todo 调试信息，之后删除
					// console.log(res);
					switch (res.statusCode) {
						case 401:
						case 403:
						case 404:
							uni.showModal({
								title: '错误提示',
								content: '没有找到网页错误'
							})
							break
						case 500:
							uni.showModal({
								title: '错误提示',
								content: '服务器发生错误'
							})
							console.error(res)
							break
						case 505:
							uni.showModal({
								title: '错误提示',
								content: '不支持的http协议'
							})
							console.error(res)
							break
						default:
							if(res.data.code === 0) {
								reslove(res.data)
							} else {
								uni.showModal({
									title:'错误提示',
									content:res.data.message
								})
							}
							break
					}
				},
				fail(err) {
					console.error(err);
					uni.showModal({
						title:'错误提示',
						content:err.message
					})
				},
				complete() {
					uni.hideLoading()
				}
			})
		}catch(err){
			uni.showModal({
				title:'错误提示',
				content: err.message
			})
		}
	})
}